<?php require_once('../Connections/CSRE_Connection.php'); ?>
<?php require_once('Connections/locamedicadmin.php'); ?>

<?php include('userValidation.php'); ?>

<?php require_once('functions.php'); ?>

<?php
$currentPage = $_SERVER["PHP_SELF"];

mysql_select_db($database_CSRE_Connection, $CSRE_Connection);
$query_comboCategories = "SELECT * FROM csre_category";
$comboCategories = mysql_query($query_comboCategories, $CSRE_Connection) or die(mysql_error());
$row_comboCategories = mysql_fetch_assoc($comboCategories);
$totalRows_comboCategories = mysql_num_rows($comboCategories);

$filterValue = "";
$category = "";
$filter = "";

$maxRows_products = 20;
$pageNum_products = 0;
if (isset($_GET['pageNum_products'])) {
  $pageNum_products = $_GET['pageNum_products'];
}
$startRow_products = $pageNum_products * $maxRows_products;

mysql_select_db($database_locamedicadmin, $locamedicadmin);
$query_products = "SELECT p.*, m.name_FR as manuname_FR, m.name_US as manuname_US, c.name_FR as catename_FR, c.name_US as catename_US FROM csre_product p, csre_manufacture m, csre_category c where p.manufacture_id = m.manufacture_id and p.category_id = c.category_id and deleted = 0";

if (isset($_POST['search']) || isset($_GET['search'])) {

	if (isset($_POST['filter'])) { $filter = addslashes($_POST['filter']); } else { $filter = addslashes($_GET['filter']); }
	if (isset($_POST['filterValue'])) { $filterValue = addslashes($_POST['filterValue']); } else { $filterValue = addslashes($_GET['filterValue']); }
	if (isset($_POST['category'])) { $category = addslashes($_POST['category']); } else { $category = addslashes($_GET['category']); }

	if ($filter == "category" || ($filter != "category" && $filterValue != "")) {
		$where = "";
		if ($filter == "numcsre") {
			$where = sprintf(" and csre_number = '%s' ", $filterValue);
		} else if ($filter == "keyword") {
			$where = "and keywords like '%" . $filterValue . "%' ";
		} else if ($filter == "nummanufact") {
			$where = sprintf(" and m.manu_num = %s ", $filterValue);
		} else if ($filter == "numlocamedic") {
			$where = sprintf(" and locamedic_number = '%s' ", $filterValue);
		} else if ($filter == "nameproduct") {
			$where = sprintf(" and p.name_%s like '%s' ", "FR", '%' . $filterValue . '%');
		} else if ($filter == "namemanuf") {
			$where = sprintf(" and m.name_%s like '%s' ", "FR", '%' . $filterValue . '%');
		} else if ($filter == "category") {
			$where = sprintf(" and c.category_id = %s ", $category);
		}
	
		$query_products = $query_products . $where;
	
	}
}

$order = "";
$orderby = "";

if (isset($_POST['order']) && isset($_POST['orderby'])) {

	$order = $_POST['order'];
	$orderby = $_POST['orderby'];
	
	$query_products = $query_products . " order by " . $order . " " . $orderby;

}





$query_limit_products = sprintf("%s LIMIT %d, %d", $query_products, $startRow_products, $maxRows_products);
$products = mysql_query($query_limit_products, $locamedicadmin) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);

if (isset($_GET['totalRows_products'])) {
  $totalRows_products = $_GET['totalRows_products'];
} else {
  $all_products = mysql_query($query_products);
  $totalRows_products = mysql_num_rows($all_products);
}
$totalPages_products = ceil($totalRows_products/$maxRows_products)-1;

$queryString_products = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_products") == false && 
        stristr($param, "totalRows_products") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_products = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_products = sprintf("&totalRows_products=%d%s", $totalRows_products, $queryString_products);
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Locamedic CSRE Admin</title>
<link href="locamedicadmin.css" rel="stylesheet" type="text/css" />

	<script>
		function showCat(itemselected) {
			var show = 'none';
			disabled = false;
			if (itemselected == 6) {
				show = 'block';
				disabled = true;
				document.getElementById('filterValue').value = "";
			}
			document.getElementById('labelcategory').style.display = show;
			document.getElementById('category').style.display = show;
			document.getElementById('filterValue').disabled = disabled;
		}
		
	</script>

</head>
<body>
<div id="page">

	<?php include('sidebar.php'); ?>
	
	<!-- end sidebar -->
	<div id="content">
		<div align="center"><img src="images/page_image_5.jpg" alt="" /></div>
	  <div class="boxed">
		<h1 class="title2">Liste des produits</h1>
		
		<table>
			<tr>
				<td>
					<form action="product_list.php" method="post">
						<input type="hidden" value="search" name="search" />
						<table cellpadding="5" class="text">
							<tr>
								<td width="150">Rechercher pour:</td>
								<td width="150">Par:</td>
								<td width="150"><span id="labelcategory" style="display:none;">Cat&eacute;gorie:</span></td>
								<td>
									Trier par
								</td>
								<td>&nbsp;
									
								</td>
							</tr>
							<tr>
								<td><input type="text" name="filterValue" id="filterValue" value="<?php echo $filterValue; ?>" 
									<?php if ($filter == 'category') { echo "disabled=\"disabled\""; } ?> /></td>
								<td>
									<select style="width: 110px;" class="text" onChange="showCat(this.selectedIndex);" name="filter" id="filter">
										<option <?php if ("numcsre" == $filter) { echo "selected=\"selected\""; } ?> value="numcsre">No. CSRE</option>
										<option <?php if ("keyword" == $filter) { echo "selected=\"selected\""; } ?> value="keyword">Mots Cle</option>
										<option <?php if ("nummanufact" == $filter) { echo "selected=\"selected\""; } ?> value="nummanufact">
											No. Manufacturier</option>
										<option <?php if ("numlocamedic" == $filter) { echo "selected=\"selected\""; } ?> value="Locamedic #">
											No. Locamedic</option>
										<option <?php if ("nameproduct" == $filter) { echo "selected=\"selected\""; } ?> value="nameproduct">
											Nom du produit</option>
										<option <?php if ("namemanuf" == $filter) { echo "selected=\"selected\""; } ?> value="namemanuf">
											Nom du Manufacturier</option>
										<option <?php if ("category" == $filter) { echo "selected=\"selected\""; } ?> value="category">
											Cat&eacute;gorie</option>
									</select>
								</td>
								<td>
									<select style="width: 180px; <?php if ($filter != 'category') { echo "display: none;"; } ?>" class="text" id="category" 
										name="category">
									  <?php do {  ?>
										<option value="<?php echo $row_comboCategories['category_id']?>"
											<?php if ($row_comboCategories['category_id'] == $category) { echo "selected=\"selected\""; } ?>>
											<?php echo $row_comboCategories['name_FR']?></option>
										<?php } while ($row_comboCategories = mysql_fetch_assoc($comboCategories)); ?>
									</select>
								</td>
								<td>
									<select name="order">
										<option <?php if ($order == 'csre_number') { echo "selected='selected' "; } ?> value="csre_number">CSRE#</option>
										<option <?php if ($order == 'locamedic_number') { echo "selected='selected' "; } ?> value="locamedic_number">Locamedic#</option>
										<option <?php if ($order == 'name_FR') { echo "selected='selected' "; } ?> value="name_FR">Nom</option>
										<option <?php if ($order == 'price') { echo "selected='selected' "; } ?> value="price">Prix</option>
										<option <?php if ($order == 'catename_FR') { echo "selected='selected' "; } ?> value="catename_FR">Cat&eacute;gorie</option>
									</select>
								</td>
								<td>
									<select name="orderby">
										<option <?php if ($orderby == 'asc') { echo "selected='selected' "; } ?> value="asc">&uarr;</option>
										<option <?php if ($orderby == 'desc') { echo "selected='selected' "; } ?>value="desc">&darr;</option>
									</select>
								</td>
							</tr>
							<tr>
								<td colspan="5">												
									<div class="botonera">
										<input type="submit" value="Rechercher" />
									</div>
								</td>
							</tr>
						</table>
					</form>
				</td>
			</tr>
		</table>
			

		<?php if ($totalRows_products > 0) { ?>		
		
		<table cellpadding="5" cellspacing="0">
		  <thead>
	        <td>CSRE #</td>
			<td>Locamedic #</td>
			<td>Nom</td>
			<td>Cat&eacute;gorie</td>
			<td>Manufacture #</td>
			<td>Prix</td>
			<td width="5%"></td>
			<td width="5%"></td>
		 </thead>
		  <?php do { ?>
			<tr>
			  <td><?php echo $row_products['csre_number']; ?></td>
			  <td><?php echo $row_products['locamedic_number']; ?></td>
			  <td><?php echo $row_products['name_' . $lang]; ?></td>
			  <td><?php echo $row_products['catename_' . $lang]; ?></td>
			  <td><?php echo $row_products['manufacture_num']; ?></td>
			  <td align="right">$ <?php echo number_format($row_products['price'],2,'.',','); ?></td>
			  <td align="center">
				<a href="product_edit.php?productid=<?php echo $row_products['product_id']; ?>">
					<img src="images/1307905892_desktop.png" alt="Modifier" border="0" />
				</a>
			  </td>
			  <td align="center">
				<a href="product_delete.php?productid=<?php echo $row_products['product_id']; ?>"
					onclick="return confirm('Etes-vous sur de vouloir supprimer ce registre?');">
					<img src="images/1307905898_edit-trash.png" alt="Supprimer" border="0" />
				</a>
			  </td>
			</tr>
			<?php } while ($row_products = mysql_fetch_assoc($products)); ?>
		</table>
		<table>
			<tr>
				<td align="center">
					Rows <?php echo ($startRow_products + 1) ?> to <?php echo min($startRow_products + $maxRows_products, $totalRows_products) ?> of <?php echo $totalRows_products ?>
				</td>
			</tr>
		</table>
		       

		
		<table border="0" width="50%" align="center">
          <tr>
            <td width="23%" align="center"><?php if ($pageNum_products > 0) { // Show if not first page ?>
                  <a href="<?php printf("%s?pageNum_products=%d%s", $currentPage, 0, $queryString_products); ?>"><img src="images/resultset_first.png" border=0></a>
                  <?php } // Show if not first page ?>
            </td>
            <td width="31%" align="center"><?php if ($pageNum_products > 0) { // Show if not first page ?>
                  <a href="<?php printf("%s?pageNum_products=%d%s", $currentPage, max(0, $pageNum_products - 1), $queryString_products); ?>"><img src="images/resultset_previous.png" border=0></a>
                  <?php } // Show if not first page ?>
            </td>
            <td width="23%" align="center"><?php if ($pageNum_products < $totalPages_products) { // Show if not last page ?>
                  <a href="<?php printf("%s?pageNum_products=%d%s", $currentPage, min($totalPages_products, $pageNum_products + 1), $queryString_products); ?>"><img src="images/resultset_next.png" border=0></a>
                  <?php } // Show if not last page ?>
            </td>
            <td width="23%" align="center"><?php if ($pageNum_products < $totalPages_products) { // Show if not last page ?>
                  <a href="<?php printf("%s?pageNum_products=%d%s", $currentPage, $totalPages_products, $queryString_products); ?>"><img src="images/resultset_last.png" border=0></a>
                  <?php } // Show if not last page ?>
            </td>
          </tr>
        </table>
		<?php } else { ?>
		<table>
			<tr>
				<td>No products found</td>
			</tr>
		</table>
		<?php } ?>
        <div class="botonera">
			<input type="button" onclick="window.location='product_add.php';" value="Add Product" />
		</div>
		
	  </div>
  </div>
	<!-- end content -->
	<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->

<?php include('footer.php'); ?>

</body>
</html>
<?php
mysql_free_result($comboCategories);

mysql_free_result($products);
?>